//
//  MineInfoEditNewTableViewCell.m
//  MJApp
//
//  Created by 王亮 on 15/7/23.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineInfoEditNewTableViewCell.h"
#import "TextPickerView.H"

typedef enum {
    
    pickerViewStateAge,
    pickerViewStateSex,
    pickerViewStateDate,
    pickerViewStateArea
} pickerViewState;

@interface MineInfoEditNewTableViewCell ()<UIPickerViewDataSource,UIPickerViewDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *arrowImageView;
@property (weak, nonatomic) IBOutlet UIView *lineView;

@property (nonatomic,strong)UIPickerView *pickerView;
@property (nonatomic,strong)UIDatePicker *datePicker;
@property (nonatomic,strong)TextPickerView *areaPickerView;
@property (nonatomic,strong)NSMutableArray *pickerArray;
@property (nonatomic,assign)pickerViewState state;

@end

@implementation MineInfoEditNewTableViewCell

- (void)awakeFromNib {
    // Initialization code
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    CGRect frame = self.bounds;
    frame.size.height = 100;
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:frame];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:frame];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    self.datePicker = datePicker;
    self.pickerView = pickerView;
    self.pickerArray = [NSMutableArray array];
    
    TextPickerView *areaPickerView = [[TextPickerView alloc] initWithFrame:frame];
    self.areaPickerView = areaPickerView;
    
    self.descTextField.delegate = self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setInfoDic:(NSDictionary *)infoDic {
    
    _infoDic = infoDic;
    self.nameLabel.text = infoDic[@"name"];
    self.descTextField.text = infoDic[@"desc"][@"value"];
    self.lineView.hidden = [infoDic[@"hidden"] boolValue];
    self.arrowImageView.hidden = [infoDic[@"arrowShow"] boolValue];
    if ([infoDic[@"name"] isEqual:@"生日"] && infoDic[@"desc"][@"value"]) {
        
        self.descTextField.text = [infoDic[@"desc"][@"value"] substringToIndex:10];
    }
    
    [self.pickerArray removeAllObjects];
    if ([infoDic[@"name"] isEqual:@"*年龄"]) {
        
        for (int i = 0; i <  200; i ++) {
            
            NSString *str = [NSString stringWithFormat:@"%d",i];
            [self.pickerArray addObject:str];
        }
        self.descTextField.inputView = self.pickerView;
    }else if ([infoDic[@"name"] isEqual:@"生日"]) {
        self.descTextField.inputView = self.datePicker;
    }else if ([infoDic[@"name"] isEqual:@"所在地"]) {
        self.areaPickerView.pickerArray = infoDic[@"areaList"];
        self.descTextField.inputView = self.areaPickerView;
    }else if ([infoDic[@"name"] isEqual:@"*性别"]) {
        
        [self.pickerArray addObject:@"男"];
        [self.pickerArray addObject:@"女"];
        self.descTextField.inputView = self.pickerView;
    }
}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return self.pickerArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    return self.pickerArray[row];
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    if ([self.infoDic[@"name"] isEqual:@"*年龄"] || [self.infoDic[@"name"] isEqual:@"*性别"]) {

        self.descTextField.text = self.pickerArray[[self.pickerView selectedRowInComponent:0]];
    }else if ([self.infoDic[@"name"] isEqual:@"生日"]) {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        NSString *dateStr = [formatter stringFromDate:self.datePicker.date];
        self.descTextField.text = dateStr;
    }else if ([self.infoDic[@"name"] isEqual:@"所在地"]) {
       
        self.descTextField.text = [self.areaPickerView selectedOfPickerView];
    }

}

@end
